﻿using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Steve.TestHelper;
using Steve.UI.Controllers;
using Steve.UI.Extensions;
using Steve.UI.Models;
using System.Collections.Generic;
using System;
using Steve.UI.Administration.Controllers;
using Steve.UI.Administration.Models;

namespace Steve.UI.Tests
{
    [TestClass]
    public class UrlManagementController_Tests : ComposablePartTests
    {
        const string testId = "siteId";
        const string testAuthority = "www.google.com";

        Site _testSite;
        SiteUrl _testUrl;

        protected override void InitializeGlobalTestData()
        {
            _testSite = StubSiteExplorerRepository.AddSite(testId, testAuthority);
            _testUrl = StubSiteExplorerRepository.AddUrl(testId, "test title", "/q");
        }

        [TestMethod]
        public void SiteExplorer_Action_Test()
        {
            var controller = this.ComposeController<UrlManagementControllerBase>();
            var result = controller.SiteExplorer();

            Assert.IsInstanceOfType(result, typeof(ViewResult));

            var viewResult = (ViewResult)result;

            Assert.AreEqual("SiteExplorer", viewResult.ViewName);
            Assert.IsInstanceOfType(viewResult.ViewData.Model, typeof(SiteExplorerModel));

            var model = (SiteExplorerModel)viewResult.ViewData.Model;

            Assert.AreEqual(1, model.Sites.Count());

            Assert.AreEqual(testId, model.Sites.First().SiteName);
            Assert.AreEqual(testAuthority, model.Sites.First().Authority);

        }

        [TestMethod]
        public void SiteUrlDetail_Action_Existing_Test()
        {
            var controller = this.ComposeController<UrlManagementControllerBase>();
            var result = controller.SiteUrlDetail(_testUrl.Id, null);

            Assert.IsInstanceOfType(result, typeof(ViewResult));

            var viewResult = (ViewResult)result;

            Assert.AreEqual("ExistingSiteUrlDetail", viewResult.ViewName);
            Assert.IsInstanceOfType(viewResult.ViewData.Model, typeof(ExistingSiteUrlDetailModel));

            var model = (ExistingSiteUrlDetailModel)viewResult.ViewData.Model;

            var expectedUrl = _testUrl.ToUri(controller.Repository);

            Assert.AreEqual(expectedUrl, model.Url);
            Assert.AreEqual(_testUrl.Start, model.Start);
            Assert.AreEqual(_testUrl.End, model.End);
        }

        [TestMethod]
        public void SiteUrlDetail_Action_New_no_Url_Test()
        {
            var controller = this.ComposeController<UrlManagementControllerBase>();
            var result = controller.SiteUrlDetail(null, null);

            var viewResult = (ViewResult)result;

            Assert.AreEqual("NewSiteUrlDetail", viewResult.ViewName);
            Assert.IsInstanceOfType(viewResult.ViewData.Model, typeof(NewSiteUrlDetailModel));

            var model = (NewSiteUrlDetailModel)viewResult.ViewData.Model;

            Assert.IsNotNull(model);
            Assert.IsNull(model.Url);
        }

        [TestMethod]
        public void SiteUrlDetail_Action_New_with_url_Test()
        {
            var controller = this.ComposeController<UrlManagementControllerBase>();
            var result = controller.SiteUrlDetail(null, "http://somesite/test");

            var viewResult = (ViewResult)result;

            Assert.AreEqual("NewSiteUrlDetail", viewResult.ViewName);
            Assert.IsInstanceOfType(viewResult.ViewData.Model, typeof(NewSiteUrlDetailModel));

            var model = (NewSiteUrlDetailModel)viewResult.ViewData.Model;

            Assert.IsNotNull(model);
            Assert.IsNotNull(model.Url);
            Assert.AreEqual("http", model.Url.Scheme);
            Assert.AreEqual("somesite", model.Url.Authority);
            Assert.AreEqual("/test", model.Url.PathAndQuery);
        }


    }
}
